package com.dobby.pea.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dobby.pea.common.BaseContext;
import com.dobby.pea.common.R;
import com.dobby.pea.dto.PurchaseDto;
import com.dobby.pea.entity.tbPurchase;
import com.dobby.pea.service.PurchaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author Dobby
 * @version 1.0
 */
@RestController
@RequestMapping("/api/purchase")
@Slf4j
public class PurchaseController {

    @Autowired
    PurchaseService purchaseService;

    /**
     * 发起采购需求
     * @param purchase
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody tbPurchase purchase){
        purchase.setUserId(BaseContext.getCurrentId());
        log.info("公布采购需求{}",purchase);
        purchaseService.save(purchase);
        return R.success("公布采购成功");

    }


    /**
     * 根据采购id返回该采购和竞标的数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<PurchaseDto> purchaseDtoById(@PathVariable Long id){
        log.info("根据采购id返回该采购和竞标的数据{}",id);
        return purchaseService.purchaseDtoById(id);
    }



    /**
     * 修改采购需求
     * @param purchase
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody tbPurchase purchase){
        log.info("修改采购需求{}",purchase);
        purchaseService.updateById(purchase);
        return R.success("修改采购成功");


    }

    /**
     * 删除采购需求(同时修改该采购订单的竞标数据的is_delete字段
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable Long id){
        log.info("删除采购需求{}",id);
        return purchaseService.deleteWithBid(id);
    }

    /**
     * 所有采购信息的分页查询(purchaseDto类型）
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/pageAll")
    public R<Page> pageAll(int page, int pageSize, String name){
        log.info("所有竞标信息的分页查询(purchaseDto类型）{}{}}{}",page,pageSize,name);
        return purchaseService.pageAll(page,pageSize,name);
    }


    /**
     * 返回该用户创建的采购(分页查询，purchaseDto类型）
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/pageUser")
    public R<Page> pageUser(int page, int pageSize, String name){
        log.info("返回该用户创建的竞标(分页查询，purchaseDto类型）{}{}{}",page,pageSize,name);
        return purchaseService.pageUser(page,pageSize,name, BaseContext.getCurrentId());

    }

}

